package com.yanqu.road.server.cmd.activity.ppl;

import com.yanqu.road.pb.activity.PplProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ppl.PplModule;
import com.yanqu.road.server.manger.activity.ppl.PplMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_ACTIVITY_PPL_RESTART, desc = "泡泡龙重新开始游戏")
public class RestartPplCmd extends AbstractCommand {

    int multiType = 1;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        if (packet.getBytes() == null) {
            return true;
        }
        PplProto.RestartPplReqMsg reqMsg = PplProto.RestartPplReqMsg.parseFrom(packet.getBytes());
        if (reqMsg.hasMultiType()) {
            multiType = reqMsg.getMultiType();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (multiType != 1 && multiType != 2 && multiType != 3) {
            ret = GameErrorCode.E_ACTIVITY_PPL_MULTI_TYPE_ERROR;
            return;
        }
        if (!PplMgr.isActivityInTime()) {
            ret = GameErrorCode.E_ACTIVITY_PPL_NO_IN_TIME;
            return;
        }
        PplModule pplModule = player.getModule(PplModule.class);
        if (!PplMgr.isGameOver(pplModule.getUserData(), multiType)) {
            ret = GameErrorCode.E_ACTIVITY_PPL_NO_GAME_OVER;
            return;
        }

        player.getModule(PplModule.class).restartGame(multiType);
    }

    @Override
    public void sendData(GamePlayer player) {
        PplProto.RestartPplRespMsg.Builder builder = PplProto.RestartPplRespMsg.newBuilder();
        builder.setRet(ret);
        player.sendPacket(ClientProtocol.U_ACTIVITY_PPL_RESTART, builder);
    }
}
